<template>
  <div>
    <el-dialog title="新增仓库" :visible.sync="addstorevis" @close="close" width="500px" append-to-body>
        <el-form :model="addStoreForm" ref="addstore">

            <el-form-item label="编号:" :label-width="formLabelWidth">
                <el-input v-model="addStoreForm.store_num" placeholder="请输入仓库编号"></el-input>
            </el-form-item>
            <el-form-item label="名称:" :label-width="formLabelWidth">
                <el-input v-model="addStoreForm.store_name" placeholder="请输入仓库名称"></el-input>
            </el-form-item>
            <el-form-item label="地址:" :label-width="formLabelWidth">
                <el-input v-model="addStoreForm.store_address" placeholder="请输入仓库地址"></el-input>
            </el-form-item>
            <el-form-item label="电话:" :label-width="formLabelWidth">
                <el-input v-model="addStoreForm.store_tel" placeholder="请输入仓库电话"></el-input>
            </el-form-item>
            <el-form-item label="队站:" :label-width="formLabelWidth">
                <el-input v-model="addStoreForm.pertain" placeholder="请输入队站所属"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="danger" :loading="submitLoading" @click="successAddstore">确 定</el-button>
        </div>
          </el-dialog>
        </div>
</template>

<script>
import { AddStore } from '@/api/store.js';

export default {
  name: 'addstore',
  props: ['addstoredialog'],
  data() {
    return {
      addstorevis: false,
      submitLoading: false,
      addStoreForm: {
        store_num: '',
        store_name: '',
        store_address: '',
        store_tel: '',
        pertain: '',
      },
      formLabelWidth: '70px',
    }
  },
  methods: {
    close() {
      this.addstorevis = false;
      this.resetForm();
      this.$emit("update:addstoredialog", false);
    },
    resetForm() {
      this.addStoreForm.store_num = '';
      this.addStoreForm.store_name = '';
      this.addStoreForm.store_address = '';
      this.addStoreForm.store_tel = '';
      this.addStoreForm.pertain = '';
    },
    successAddstore() {
        if (!this.addStoreForm.store_num || !this.addStoreForm.store_name 
            || !this.addStoreForm.store_address || !this.addStoreForm.store_tel || !this.addStoreForm.pertain) {
            this.$message.error('请输入完整信息')
        } else {
            let reqData = {
                number: this.addStoreForm.store_num,
                name: this.addStoreForm.store_name,
                address: this.addStoreForm.store_address,
                tel: this.addStoreForm.store_tel,
                pertain: this.addStoreForm.pertain,
            }
            this.submitLoading = true;
            AddStore(reqData).then(response => {
                let data = response.data
                this.$message({
                    message: data.msg,
                    type: 'success'
                })
                this.submitLoading = false;
                this.$refs.addstore.resetFields();
                this.resetForm();
            }).catch(error => {
                this.submitLoading = false;
            })
        }
    }
  },
  watch: {
    addstoredialog(newVal,oldVal) {  
      this.addstorevis = newVal;
    }
  }
}
</script>

<style lang="scss" scoped>
  
</style>